import os
import re
import scipy.io as scio
from PyCmpltrtok.common import sep, get_dir_name_ext, remove_tail_newline

sep('Load and parse')
path = r'D:\_const\large_data\face\bfm\__workspace\PublicMM1\11_feature_points\Farkas_face05.fp'
print(f'Loading {path}')
landmarks = []
with open(path, 'r') as f:
    while True:
        line = f.readline()
        if not line:
            break
        if line[0] < '0' or line[0] > '9':
            continue
        line = remove_tail_newline(line)
        args = re.split(r'\s+', line)
        coord = [float(args[1]), float(args[2]), float(args[3])]
        landmarks.append(coord)
print('Loaded and parsed.')

sep('Save to matlab format')
dir, base, ext = get_dir_name_ext(os.path.abspath(__file__))
savedir = os.path.join(dir, '_save', base)
os.makedirs(savedir, exist_ok=True)
savepath = os.path.join(savedir, 'landmarks.mat')
print(f'Saving to {savepath}')
scio.savemat(savepath, {'landmarks': landmarks})
print('Saved')

sep('All over')
